package org.jcms.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Book;
import org.jcms.model.PageBean;
import org.jcms.service.BookServiceI;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ManagerSearchBooks extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ManagerSearchBooks() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		BookServiceI bookServiceI = (BookServiceI) ac
				.getBean("bookServiceImpl");

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String type = request.getParameter("type");
		String bookIsbn = request.getParameter("bookIsbn");
		String bookName = request.getParameter("bookName");
		String retJson = "";
		int intPage = 1;
		int intRows = 10;
		int intType = 0;

		JSONObject jsonObject = new JSONObject();

		try {
			intPage = Integer.parseInt(page);
			intRows = Integer.parseInt(rows);
			intType = Integer.parseInt(type);
			if (intPage <= 0) {
				intPage = 1;
			}
			if (intRows > 200) {
				intRows = 10;
			}
			if (intType > 3 || intType < 0) {
				intType = 0;
			}
			List<Book> books = null;
			PageBean<Book> pbBook = null;
			String hql = "from Book";
			switch (intType) {
			case 0:
				hql = "from Book order by bookIsbn";
				pbBook = bookServiceI.findByPage(hql, intPage, intRows);
				break;
			case 1:
				hql = "from Book where bookStock < bookOrderNumber order by bookIsbn";
				pbBook = bookServiceI.findByPage(hql, intPage, intRows);
				break;
			case 2:
				hql = "from Book where bookName like ? order by bookIsbn";
				pbBook = bookServiceI.findByPage(hql, intPage, intRows, "%"
						+ bookName + "%");
				break;
			case 3:
				hql = "from Book where bookIsbn = ? order by bookIsbn";
				pbBook = bookServiceI.findByPage(hql, intPage, intRows,
						bookIsbn);
				break;
			default:
				break;
			}
			jsonObject.put("total", pbBook.getTotalRows());
			books = pbBook.getData();
			List<HashMap<String, String>> bookls = new ArrayList<HashMap<String, String>>();
			for (Book book : books) {
				HashMap<String, String> tempMap = new HashMap<String, String>();
				tempMap.put("bookIsbn", book.getBookIsbn());
				tempMap.put("bookName", book.getBookName());
				tempMap.put("bookPrice", String.valueOf(book.getBookPrice()));
				tempMap.put("bookAuthor", book.getBookAuthor());
				tempMap.put("bookPublisher", book.getBookPublisher());
				tempMap.put("bookStock", book.getBookStock() + "");
				tempMap.put("bookOrderNumber", book.getBookOrderNumber() + "");
				tempMap.put(
						"bookNeed",
						book.getBookStock() - book.getBookOrderNumber() >= 0 ? "不缺"
								: "缺"
										+ (book.getBookOrderNumber() - book
												.getBookStock()) + "本");
				bookls.add(tempMap);
			}
			jsonObject.put("rows", bookls);
			retJson = jsonObject.toString();
		}

		catch (Exception e) {

		}

		finally {
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
